import { listBrand, pageBrand } from '@/api/brand'

const state = {
  brands: [],
  brandDir:[]
}

const mutations = {

  PAGE_BRAND: (state, data) => {
    state.brands = data
  },
  LIST_BRAND:(state, data) => {
    state.brandDir = data
  }
}

const actions = {
  listBrand({ commit, state }) {
    return new Promise((resolve, reject) => {
      listBrand().then(data=> {
        const result = data.map(n => {
          return {
            label: n.name,
            value: n.id
          }
        })
        commit('LIST_BRAND',result)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  pageBrand({ commit, state },_data) {
    return new Promise((resolve, reject) => {
      pageBrand(_data).then(data=> {
        commit('PAGE_BRAND',data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

